import os
import subprocess
import time

import psutil
import win32com.client
import wmi


"""
    获取电脑运行参数
"""


def get_computer_parameters():
    # # 获取CPU占比
    cpu_usage = psutil.cpu_percent(interval=1)
    print("CPU 占比：{}%".format(cpu_usage))
#     #
#     # # 获取内存占比
#     # memory = psutil.virtual_memory()
#     # memory_usage = memory.percent
#     # print("内存占比：{}%".format(memory_usage))
#     #
#     # # 获取硬盘占比
#     # disk = psutil.disk_usage('/')
#     # disk_usage = disk.percent
#     # print("硬盘占比：{}%".format(disk_usage))
#     #
#     # # 获取网速
#     # bytes_sent_before = psutil.net_io_counters().bytes_sent
#     # bytes_recv_before = psutil.net_io_counters().bytes_recv
#     # time.sleep(1)  # 等待1秒钟
#     # bytes_sent_after = psutil.net_io_counters().bytes_sent
#     # bytes_recv_after = psutil.net_io_counters().bytes_recv
#     #
#     # # sent_speed = (bytes_sent_after - bytes_sent_before) / 1024 / 1024
#     # # recv_speed = (bytes_recv_after - bytes_recv_before) / 1024 / 1024
#     # #
#     # # print("发送速度：{} MB/s".format(sent_speed))
#     # # print("接收速度：{} MB/s".format(recv_speed))
#     #
#     # sent_speed = (bytes_sent_after - bytes_sent_before) / 1024
#     # recv_speed = (bytes_recv_after - bytes_recv_before) / 1024
#     #
#     # print("发送速度：{} KB/s".format(sent_speed))
#     # print("接收速度：{} KB/s".format(recv_speed))

    # 获取系统虚拟内存信息
    memory_info = psutil.virtual_memory()

    # 提取总内存、已使用内存、空闲内存的值（单位为字节）
    total_memory_bytes = memory_info.total
    used_memory_bytes = memory_info.used
    free_memory_bytes = memory_info.available  # 注意这里使用available而不是free

    # 转换为MB和GB
    total_memory_mb = total_memory_bytes / 1024 ** 2  # 转换成MB
    used_memory_mb = used_memory_bytes / 1024 ** 2  # 转换成MB
    free_memory_mb = free_memory_bytes / 1024 ** 2  # 转换成MB

    total_memory_gb = total_memory_bytes / 1024 ** 3  # 转换成GB
    used_memory_gb = used_memory_bytes / 1024 ** 3  # 转换成GB
    free_memory_gb = free_memory_bytes / 1024 ** 3  # 转换成GB

    # 打印转换后的内存信息
    print(f"总内存：{total_memory_mb:.2f} MB / {total_memory_gb:.2f} GB")
    print(f"已使用内存：{used_memory_mb:.2f} MB / {used_memory_gb:.2f} GB")
    print(f"空闲内存：{free_memory_mb:.2f} MB / {free_memory_gb:.2f} GB")


    # 获取根目录磁盘使用情况信息
    disk_info = psutil.disk_usage('/')

    # 提取总容量、已使用容量、可用容量的值（单位为字节）
    total_bytes = disk_info.total
    used_bytes = disk_info.used
    free_bytes = disk_info.free

    # 转换为GB、MB、TB
    total_gb = total_bytes / (1024 ** 3)  # 转换成GB
    used_gb = used_bytes / (1024 ** 3)  # 转换成GB
    free_gb = free_bytes / (1024 ** 3)  # 转换成GB

    # 打印转换后的磁盘使用情况信息
    print(f"总容量：{total_gb:.2f} GB")
    print(f"已使用容量：{used_gb:.2f} GB")
    print(f"可用容量：{free_gb:.2f} GB")


    # 获取网络IO计数器信息
    network_info = psutil.net_io_counters()

    # 提取信息
    bytes_sent = network_info.bytes_sent
    bytes_recv = network_info.bytes_recv
    packets_sent = network_info.packets_sent
    packets_recv = network_info.packets_recv
    errin = network_info.errin
    errout = network_info.errout
    dropin = network_info.dropin
    dropout = network_info.dropout

    # 打印信息
    print(f"已发送字节数: {bytes_sent} bytes")
    print(f"已接收字节数: {bytes_recv} bytes")
    print(f"已发送数据包数量: {packets_sent} packets")
    print(f"已接收数据包数量: {packets_recv} packets")
    print(f"接收时错误数据包数量: {errin} packets")
    print(f"发送时错误数据包数量: {errout} packets")
    print(f"接收时丢弃的数据包数量: {dropin} packets")
    print(f"发送时丢弃的数据包数量: {dropout} packets")



# while True:
#     get_computer_parameters()
#     time.sleep(5)

import wmi

w = wmi.WMI()

for Memory in w.Win32_PhysicalMemory():  # 获取内存信息
    print(Memory)

for CPU in w.Win32_Processor():  # 获取CPU信息
    print(CPU)

for Disk in w.Win32_LogicalDisk():  # 获取硬盘信息
    print(Disk)

for Net in w.Win32_NetworkAdapterConfiguration():  # 获取网卡信息
    print(Net)
